//2.	Load a 2D map from a text file into a dynamically allocated 2D array.
//a.	As a test map, use the example provided to the right of this text.  Note that the first two values are width and height of the map.
//b.	Hint: you may need to manually ignore character 13 (linefeed).
//c.	For information on 2D memory allocation, see http://www.geocities.com/varunhostel/TechnicalArticles/PointerArticle/PointerArticle_Part2.html?reload_coolmenus#pos9
//	8 7
// aaaaa  
//     aa 
//     aa 
// aaaaaa 
//aa   aa 
//aa   aa 
// aaaa aa
//

//#define PRIMER2

#ifdef PRIMER2

#include <iostream>
#include <fstream>

#define NUM_CHARS	256
#define MAX_LINE	256

int main()
{
	char buffer[NUM_CHARS], c;
	std::ifstream myFile;
	int height, width;

	char ** myData;

	std::cout << "Enter the name of the text file" << std::endl;

	std::cin.get(buffer, NUM_CHARS);

	myFile.open(buffer);

	if(myFile.good())
	{
		myFile.getline(buffer, MAX_LINE);
		bool heightFound = false;

		for(int i = 0; buffer[i] != '\n'; ++i)
		{
			int num = buffer[i] - '0';

			// valid number
			if (num > 0 && num <= 9)
			{	
				if (!heightFound)
				{
					// found the height
					height = num;
					heightFound = true;
				}
				else
				{
					// found the width
					width = num;
					break; // done!
				}
			}
		}

		// create the dynamically allocated array
		myData = new char*[height];
		for(int i = 0; i < height; ++i)
		{
			myData[i] = new char[width];
		}

		// read the rest of the file
		int lineNum = 0;

		while(!myFile.eof())
		{
			myFile.getline(buffer, MAX_LINE); // gets a character from the file

			for(int i = 0; i < strlen(buffer); ++i)
			{
				myData[lineNum][i] = buffer[i];
			}
			++lineNum;
		}
	}
	else
	{
		std::cout << "Error: File corrupted or does not exist" << std::endl;
		return 0;
	}

	printf("This is the data we collected:\n");

	for(int i = 0; i < height; i++)
	{
		for(int j = 0; j < width; j++)
		{
			printf("%c", myData[i][j]);
		}
		printf("\n");
	}

	myFile.close();

	return 0;
}

#endif